import { gameConfigHandle } from 'core/config/gameConfig';
import { FighterModel } from 'core/model/fighterModel';
import { PartnerModel } from 'core/model/partnerModel';
import { PlayerModel } from 'core/model/playerModel'
import { BagType, MapType } from 'core/typeDefine/typeDefine';
import bagHandle from './bagHandle';
import fighterPartnerHandle from './fighterPartnerHandle';
import adventureHandle from './adventureHandle';
import { BossMapDataModel } from 'core/model/adventureModel';
import luckHandle from './luckHandle';
import Random from 'core/utils/random';
import utils from 'core/utils/utils';

class PlayerHandle {
    createPlayer(time): PlayerModel {
        let player = new PlayerModel()
        // this.initPartner(player)
        let baseConfig = gameConfigHandle.gameConfig.base
        player.adventure.getResourceTime = time
        player.adventure.autoTime = time
        player.adventure.offlineTime = time
        player.adventure.mapID = baseConfig.initMapID
        return player
    }
    initPartner(player: PlayerModel) {
        let partner = new PartnerModel()
        let baseConfig = gameConfigHandle.gameConfig.base
        partner.tid = baseConfig.initPartnerTID
        partner = new PartnerModel()
        partner.tid = baseConfig.initPartnerTID
        partner.indexID = player.team.partnerIndexID++
        player.team.list.push(partner)
    }
    initPlayer(player: PlayerModel, isMonthVip: boolean, isYearVip: boolean): PlayerModel {
        let { vipLvl } = this.getVip(player)
        player.bag.type = BagType.Bag
        bagHandle.initPackag(player.bag)
        player.equipmentBag.type = BagType.Equipment
        bagHandle.initPackag(player.equipmentBag)
        player.petBag.type = BagType.Pet
        bagHandle.initPackag(player.petBag)
        // player.skillBag.type = BagType.Skill
        // bagHandle.initPackag(player.skillBag)
        this.initTeamFighterAttri(player, isMonthVip, isYearVip)
        adventureHandle.calcTalentResoureAdd(player)
        adventureHandle.calcTreasureAdventureAdd(player)
        let must = this.isMustAdjustTeam(player)
        if (must == 1) {
            if (player.team.teamList[4]) {
                let index = -1
                for (let i = 5; i < 10; i++) {
                    if (!player.team.teamList[i]) {
                        index = i
                        break;
                    }
                }
                player.team.teamList[index] = player.team.teamList[4]
                player.team.teamList[4] = null

            }
        } else if (must == 2) {
            if (player.team.teamList[9]) {
                let index = -1
                for (let i = 0; i < 5; i++) {
                    if (!player.team.teamList[i]) {
                        index = i
                        break;
                    }
                }
                player.team.teamList[index] = player.team.teamList[9]
                player.team.teamList[9] = null

            }
        }

        return player
    }

    initTeamFighterAttri(player: PlayerModel, isMonthVip: boolean, isYearVip: boolean) {
        let fighterTeamInfo = fighterPartnerHandle.initPartnerFighterTeam(player, false, MapType.Normal, isMonthVip, isYearVip)
        let totalPower = 0
        fighterTeamInfo.team.forEach((fighter: FighterModel) => {
            if (fighter) {
                totalPower += fighter.power
            }
        })
        player.baseInfo.power = totalPower
    }


    resetDay(player: PlayerModel, time, dayTime) {
        player.market.freeTimes = 0
        player.market.refreshTimes = 0
        player.market.buyDayAmount = 0
        player.adventure.mapDayChallenge = {}
        player.adventure.abyssAmount = 0
        player.adventure.instanceAmount = 0
        player.baseInfo.luckXunbaoTimes = 0
        player.baseInfo.daySign = false
        player.baseInfo.newYearSign = false
        player.task.dayMession = {}
        player.task.getDayMessionGift = {}
        player.task.dayMessionGiftsState = [false, false, false, false, false]
        player.task.gemUpTimes = 0
        player.baseInfo.gameDaySign = false
        player.baseInfo.clickGetGoldTimes = 0
        player.baseInfo.getOnlineDataTimes = 0
        player.baseInfo.dayArenaTimes = 0
        player.baseInfo.dayChestTimes = 0
        player.baseInfo.monthVipSign = false
        player.baseInfo.yearVipSign = false
        player.baseInfo.shareSign = false
        player.baseInfo.shareSign1 = false
        player.adventure.bossMapData = new BossMapDataModel()
        luckHandle.resetCard(player, new Random())
        player.luck.card.flipTimes = 0
        player.baseInfo.setArenaTimes = 0
        player.luck.maze.actionPower = 0
        player.luck.maze.buyActionPower = 0
        player.luck.maze.addActionPower = 0
        player.luck.worship.buyTimes = 0
        player.luck.worship.times = 0
        player.itemInfo.worship = utils.getIncValues(gameConfigHandle.gameConfig.luck.worshipGetPoint, player.luck.worship.lvl)

        player.luck.front.buyTimes = 0
        player.luck.front.deadHeros = []//320605
        player.luck.front.haveGetScore = false
        player.luck.front.times = 0

        player.luck.trade.buyTimes = 0
        player.luck.trade.times = 0
        luckHandle.resetTradeGoods(player, new Random())

        player.market.worshipNotice = false

        player.spirit.refreshTimes = 0
        player.adventure.areaCsReward = 0
    }

    resetWeek(player: PlayerModel, weekTime, weekDay) {

    }

    getVip(player: PlayerModel) {
        let vipConfig = gameConfigHandle.gameConfig.vip
        let exp = player.itemInfo.vipExp
        let vipLvl = 0
        let needExp = vipConfig.needExp
        for (let i = 0; i < needExp.length; i++) {
            let money = needExp[i]
            if (exp < money) {
                vipLvl = i
                break;
            }
        }
        if (vipLvl >= vipConfig.needExp.length - 2) {
            vipLvl = vipConfig.needExp.length - 2
        }
        return { exp: exp, vipLvl: vipLvl }
    }


    getFirstPartnerIndexID(player: PlayerModel) {
        for (let i = 0; i < player.team.teamList.length; i++) {
            let partnertid = player.team.teamList[i]
            if (partnertid) {
                return partnertid
            }
        }
        return null
    }
    getFriendshipSubAmount(player: PlayerModel, id: number) {
        let subAmount = 1
        let addSubAttriLvl = gameConfigHandle.gameConfig.friendship.addSubAttriLvl
        let lvl = player.friendship.friendshipLvls[id] || 0
        if (lvl >= addSubAttriLvl) {
            subAmount = 2
        }
        return subAmount
    }
    getPartnerByIndexID(player: PlayerModel, indexID: number, isFront?: boolean): PartnerModel {
        // for (let i = 0; i < player.team.list.length; i++) {
        //     let partner = player.team.list[i]
        //     if (partner && partner.indexID == indexID) {
        //         return partner
        //     }
        // }
        // return null
        let partner = player.team.recruitList[indexID]
        if (!partner) {
            partner = new PartnerModel()
            partner.tid = indexID
            player.team.recruitList[indexID] = partner
        }
        if (isFront) {
            partner = player.team.frontRecruitList[indexID]
            if (!partner) {
                partner = new PartnerModel()
                partner.tid = indexID
                player.team.frontRecruitList[indexID] = partner
            }
        }
        return partner
    }
    getPartner(player: PlayerModel) {
        for (let i = 0; i < player.team.list.length; i++) {
            let partner = player.team.list[i]
            if (partner) {
                return partner
            }
        }
        return null
    }

    addItem(player: PlayerModel, item: string, amount: number, type?: number) {

    }

    isMustAdjustTeam(player: PlayerModel, isFront?: boolean) {
        let amount1 = 0, amount2 = 0
        for (let i = 0; i < 10; i++) {
            let heroTid = player.team.teamList[i]
            if (isFront) {
                heroTid = player.team.frontTeamList[i]
            }
            if (heroTid) {
                if (i <= 4) {
                    amount1 += 1
                } else {
                    amount2 += 1
                }
            }
        }
        if (amount1 >= 5) {
            return 1
        } else if (amount2 >= 5) {
            return 2
        } else {
            return 0
        }
    }

}

let playerHandle = new PlayerHandle()
export default playerHandle

